using KinonekoSoftware.CodariaDB.Documents.Personalities;

namespace KinonekoSoftware.Studio.Models.DataParts.Personality
{
    public sealed class SurveySetPresenter : ObservableObject
    {
        public SurveySetPresenter(SurveySets container)
        {
            Container = container;
            Items     = new ObservableCollection<Survey>();

            AddItemCommand       = new ActionCommand(DoAddItemCommand);
            EditItemCommand      = new FuncCommand<Survey>(DoEditItemCommand);
            ShiftUpItemCommand   = new FuncCommand<Survey>(DoShiftUpItemCommand);
            ShiftDownItemCommand = new FuncCommand<Survey>(DoShiftDownItemCommand);
            RemoveItemCommand    = new FuncCommand<Survey>(DoRemoveItemCommand);

            Items.AddMany(container.Items, true);
        }

        public SurveySets Container { get; }
        
        
        public string Name
        {
            get => Container.Name;
            set
            {
                Container.Name = value;
                RaiseUpdated();
            }
        }

        public string Intro
        {
            get => Container.Intro;
            set
            {
                Container.Intro = value;
                RaiseUpdated();
            }
        }

        private async void DoAddItemCommand()
        {
            var r = await ViewModelBase.SingleText(TitleSR.New, "新建");

            if (!r.IsFinished)
            {
                return;
            }

            var group = new Survey
            {
                Id   = ID.Get(),
                Name = r.Value,
            };

            Items.Add(group);
            Container.Items.Add(group);
        }


        private async void DoEditItemCommand(Survey target)
        {
            if (target is null)
            {
                return;
            }

            var r = await ViewModelBase.SingleText(TitleSR.Edit, "编辑");

            if (!r.IsFinished)
            {
                return;
            }

            target.Name = r.Value;
            
            
            r = await ViewModelBase.MultiText(TitleSR.Edit, "编辑");
            
            if (!r.IsFinished)
            {
                return;
            }

            target.Intro = r.Value;
        }

        private void DoShiftUpItemCommand(Survey target)
        {
            if (target is null)
            {
                return;
            }

            Items.ShiftUp(target);
            Container.Items.ShiftUp(target);
        }

        private void DoShiftDownItemCommand(Survey target)
        {
            if (target is null)
            {
                return;
            }

            Items.ShiftDown(target);
            Container.Items.ShiftDown(target);
        }

        private async void DoRemoveItemCommand(Survey target)
        {

            if (target is null)
            {
                return;
            }

            if (!await ViewModelBase.RemoveItem())
            {
                return;
            }

            Items.Remove(target);
            Container.Items.Remove(target);
        }


        public ICommandEX AddItemCommand       { get; }
        public ICommandEX EditItemCommand      { get; }
        public ICommandEX ShiftUpItemCommand   { get; }
        public ICommandEX ShiftDownItemCommand { get; }
        public ICommandEX RemoveItemCommand    { get; }

        public ObservableCollection<Survey> Items { get; }
    }
}